package com.yihuier.yihuier.Service.MeetingManager;

import com.yihuier.yihuier.Controller.UserManager.UserController;
import com.yihuier.yihuier.Mapper.MeetingManager.MeetingAttendMapper;
import com.yihuier.yihuier.Mapper.MeetingManager.MeetingMapper;
import com.yihuier.yihuier.Mapper.MeetingManager.MeetingRoomMapper;
import com.yihuier.yihuier.Mapper.MeetingManager.MeetingUserMapper;
import com.yihuier.yihuier.Util.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MeetingUserService {

    @Autowired
    private MeetingUserMapper meetingUserMapper;

    @Autowired
    private MeetingAttendMapper meetingAttendMapper;

    @Autowired
    private MeetingMapper meetingMapper;

    @Autowired
    private UserController userController;

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;


    public boolean addOneRecordWithOutRole(int user_id,int meeting_id){
        return this.meetingUserMapper.addOneRecordOfMeetingUserWithOutRole(user_id, meeting_id) > 0;
    }

    public boolean addOneRecordWithRole(int user_id,int meeting_id,int role){
        return this.meetingUserMapper.addOneRecordOfMeetingUserWithRole(user_id, meeting_id, role) > 0;
    }
    
    public int[] gettAttendUsersIds(Long meeting_id){
        return meetingUserMapper.selectAttendUsers(meeting_id);
    }

    /**
     * 得到与我有关的会议
     * */
    public List<Map<String,Object>> getRelateToMeMeetings(int user_id){
        return meetingUserMapper.selectRelateToMeMeetings(user_id);
    }

    /**
     * 根据会议编号获取参会人员编号数组
     * */
    public List<Integer> getMeetingUserIds(int meeting_id){
        return meetingUserMapper.selectMeetingUserIds(meeting_id);
    }

    /**
     * 获取用户有的会议
     * @param user_id 用户id
     * @return 所有会议的ID
     */
    public int[] getMeetingOfUserHave(int user_id){
        return meetingUserMapper.selectMeetingsOfUserHave(user_id);
    }

    /**
     * 用户申请参加某会议
     * */
    public int insertUserApplyToAttendMeeting(int user_id,int meeting_id){

        return meetingUserMapper.insertUserApplyToAttendMeeting(user_id,meeting_id);
    }

    public boolean auditUserJoinRequest(int manager_id,int user_id, int meeting_id, int status) {
        this.meetingUserMapper.updateUserRequest(user_id,meeting_id,status);
        if (status == 1){
            // 审核通过
            if (this.meetingAttendMapper.selectThisUserIfJoinMeeting(user_id,meeting_id) <= 0){
                this.meetingAttendMapper.addOneRecordOfMeetingUserWithOutReason(user_id,meeting_id);
            }

            this.meetingMapper.addOneUserIntoMeeting(meeting_id);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 通知用户
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String meetingName = meetingMapper.selectMeetingNameById(meeting_id);
                    Date transStartTime = meetingRoomMapper.selectMeetingTimeByMeetingID(meeting_id);
                    int lastTime = meetingRoomMapper.selectMeetingDuringTimeByMeetingID(meeting_id);

                    userController.sendMessage(manager_id,new int[]{user_id},meetingName,sdf.format(transStartTime), String.valueOf(lastTime));
                    // 生成二维码
                    QRCodeUtil.zxingCodeCreate("0," + meeting_id,"0" + meeting_id,250);
                }
            }).start();
            return true;
        }else if (status == 3){
            // 审核不通过
            return true;
        }else {
            return false;
        }
    }

    public List<Map<String,Integer>> getMeetingApplyUsers(int[] meeting_ids){
        return meetingUserMapper.selectMeetingApplyUsers(meeting_ids);
    }

    public int checkApply(int user_id,int meeting_id,int status){
        return meetingUserMapper.updateUserRequest(user_id,meeting_id,status);
    }
}
